package com.training.java.redbox.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.training.java.redbox.domain.Customer;
import com.training.java.redbox.domain.CustomerDAO;
import com.training.java.redbox.domain.DAOException;

@Service
public class CustomerServiceImpl implements CustomerService{

	@Autowired
	private CustomerDAO dao;
	
	@Override
	public boolean checkCustomerExistence(String username) throws ServiceException {

		Customer customer = null;
		
		try {
			customer = dao.findCustomerByUsername(username);
		} catch (DAOException e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
		if (customer==null) {
			
			return false;
			
		} else {
			
			return true;

		}
		
		
	}

	@Override
	public void addCustomer(long customerId, String firstName, String lastName,
			String address, String city, String state, String zipCode,
			String userName, String password) throws ServiceException {

		Customer customer = new Customer(customerId, firstName, lastName);
		customer.setAddress(address);
		customer.setCity(city);
		customer.setState(state);
		customer.setZipCode(zipCode);
		customer.setUserName(userName);
		customer.setPassword(password);
		
		try {
			
			dao.addCustomer(customer);
			
		} catch (DAOException e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

	@Override
	public Customer findByUsernameAndPassword(String username, String password)
			throws ServiceException {
		
		try {

			return dao.findByUsernameAndPassword(username, password);

		} catch (DAOException e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}

	
}
